package com.example.myyiyanmengjava.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.example.mvplibrary.adapter.BaseAdapter;
import com.example.mvplibrary.adapter.BaseViewHolder;
import com.example.myyiyanmengjava.R;
import com.example.myyiyanmengjava.bean.Person;

import java.util.ArrayList;
import java.util.List;

public class SheetAdapter extends BaseAdapter<Person> {

    private static final String TAG = "朱勇闯adapter";
    private String da_an;
    private TextView titleA;
    private TextView descA;
    private TextView titleB;
    private TextView descB;
    private TextView descC;
    private TextView titleD;
    private TextView descD;
    private TextView titleC;
    private boolean selected;
    private TextView titleE;
    private TextView descE;

    public SheetAdapter(List<Person> datas, Context context, int itemlayoutId) {
        super(datas, context, itemlayoutId);
    }

    @Override
    protected void bindData(BaseViewHolder holder, Person listBean, int position) {
        TextView title = holder.getView(R.id.tv_item_sheet_title);
        TextView xiangjie = holder.getView(R.id.tv_item_sheet_xiangjie);
        TextView daan = holder.getView(R.id.tv_item_sheet_daan);
        ConstraintLayout conA = holder.getView(R.id.conA);
        ConstraintLayout conB = holder.getView(R.id.conB);
        ConstraintLayout conC = holder.getView(R.id.conC);
        ConstraintLayout conD = holder.getView(R.id.conD);
        ConstraintLayout conE = holder.getView(R.id.conE);
        titleA = holder.getView(R.id.tv_item_sheet_titleA);
        descA = holder.getView(R.id.tv_item_sheet_descA);
        titleB = holder.getView(R.id.tv_item_sheet_titleB);
        descB = holder.getView(R.id.tv_item_sheet_descB);
        titleC = holder.getView(R.id.tv_item_sheet_titleC);
        descC = holder.getView(R.id.tv_item_sheet_descC);
        titleD = holder.getView(R.id.tv_item_sheet_titleD);
        descD = holder.getView(R.id.tv_item_sheet_descD);
        descE = holder.getView(R.id.tv_item_sheet_descE);
        titleE = holder.getView(R.id.tv_item_sheet_titleE);
        xiangjie.setVisibility(View.GONE);
        daan.setVisibility(View.GONE);
        conE.setVisibility(View.GONE);

        title.setText(listBean.getName());
        xiangjie.setText(listBean.getXiang_jie());
        da_an = listBean.getDa_an();
        daan.setText(da_an);

        ArrayList<String> list = listBean.getStrings();

        selected = true;
        titleA.setText(list.get(0));
        titleB.setText(list.get(1));
        titleC.setText(list.get(2));
        titleD.setText(list.get(3));
        if (list.size() == 5) {
            Log.e(TAG, "bindData: " + list.get(4));
            titleE.setText(list.get(4));
            conE.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    daan.setVisibility(View.VISIBLE);
                    xiangjie.setVisibility(View.VISIBLE);
                    if (selected) {
                        if (daan.equals("E")) {
                            descE.setText("");
                            descE.setBackgroundResource(R.drawable.ic_true);
                            titleE.setTextColor(Color.BLUE);
                        } else {
                            descE.setText("");
                            descE.setBackgroundResource(R.drawable.ic_false);
                            titleE.setTextColor(Color.RED);
                            initBoolean();
                        }
                        selected = false;
                    }
                }
            });
        }
        if (listBean.getTi_type().equals("1")) {

            conA.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    daan.setVisibility(View.VISIBLE);
                    xiangjie.setVisibility(View.VISIBLE);
                    if (selected) {
                        if (daan.equals("A")) {
                            descA.setText("");
                            descA.setBackgroundResource(R.drawable.ic_true);
                            titleA.setTextColor(Color.BLUE);
                        } else {
                            descA.setText("");
                            descA.setBackgroundResource(R.drawable.ic_false);
                            titleA.setTextColor(Color.RED);
                            initBoolean();
                        }
                        selected = false;
                    }
                }
            });
        }

        conB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daan.setVisibility(View.VISIBLE);
                xiangjie.setVisibility(View.VISIBLE);
                if (selected) {
                    if (daan.equals("B")) {
                        descB.setText("");
                        descB.setBackgroundResource(R.drawable.ic_true);
                        titleB.setTextColor(Color.BLUE);
                    } else {
                        descB.setText("");
                        descB.setBackgroundResource(R.drawable.ic_false);
                        titleB.setTextColor(Color.RED);
                        initBoolean();
                    }
                    selected = false;
                }
            }
        });


        conC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daan.setVisibility(View.VISIBLE);
                xiangjie.setVisibility(View.VISIBLE);
                if (selected) {
                    if (daan.equals("C")) {
                        descC.setText("");
                        descC.setBackgroundResource(R.drawable.ic_true);
                        titleC.setTextColor(Color.BLUE);
                    } else {
                        descC.setText("");
                        descC.setBackgroundResource(R.drawable.ic_false);
                        titleC.setTextColor(Color.RED);
                        initBoolean();
                    }
                    selected = false;
                }
            }
        });


        conD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                daan.setVisibility(View.VISIBLE);
                xiangjie.setVisibility(View.VISIBLE);
                if (selected) {
                    if (daan.equals("D")) {
                        descD.setText("");
                        descD.setBackgroundResource(R.drawable.ic_true);
                        titleD.setTextColor(Color.BLUE);
                    } else {
                        descD.setText("");
                        descD.setBackgroundResource(R.drawable.ic_false);
                        titleD.setTextColor(Color.RED);
                        initBoolean();
                    }
                    selected = false;
                }
            }
        });

    }

    private void initBoolean() {
        switch (da_an) {
            case "A":
                descA.setText("");
                descA.setBackgroundResource(R.drawable.ic_true);
                titleA.setTextColor(Color.BLUE);
                break;
            case "B":
                descB.setText("");
                descB.setBackgroundResource(R.drawable.ic_true);
                titleB.setTextColor(Color.BLUE);
                break;
            case "C":
                descC.setText("");
                descC.setBackgroundResource(R.drawable.ic_true);
                titleC.setTextColor(Color.BLUE);
                break;
            case "D":
                descD.setText("");
                descD.setBackgroundResource(R.drawable.ic_true);
                titleD.setTextColor(Color.BLUE);
                break;
            case "E":
                descE.setText("");
                descE.setBackgroundResource(R.drawable.ic_true);
                titleE.setTextColor(Color.BLUE);
                break;
        }
    }

}
